package com.changgou.order.service.impl;

import com.changgou.entity.Result;
import com.changgou.goods.feign.SkuFeign;
import com.changgou.goods.feign.SpuFeign;
import com.changgou.goods.pojo.Sku;
import com.changgou.goods.pojo.Spu;
import com.changgou.order.pojo.OrderItem;
import com.changgou.order.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CartServiceImpl implements CartService {
    private static final String CART = "Cart_";

    @Autowired
    private RedisTemplate redisTemplate;//因为购物车在Redis中的存储结构为Map结构,所以需要使用redisTemplate模板,如果是String格式的,可以使用StringRedisTemplate模板

    @Autowired
    private SkuFeign skuFeign;

    @Autowired
    private SpuFeign spuFeign;

    @Override
    public void add(String skuId, Integer num, String username) {
        //查询Redis中的数据.
        OrderItem orderItem = (OrderItem) redisTemplate.boundHashOps(CART + username).get(skuId);

        //如果Redis中已经有了,则追加数量,重新计算金额.
        if (orderItem != null) {
            orderItem.setNum(orderItem.getNum() + num);
            //当购物车中的商品数量为0时,删除该商品
            if (orderItem.getNum() <= 0) {
                redisTemplate.boundHashOps(CART + username).delete(skuId);
                return;
            }
            orderItem.setMoney(orderItem.getPrice() * orderItem.getNum());
            orderItem.setPayMoney(orderItem.getPrice() * orderItem.getNum());

        } else {
            //如果Redis中没有,则将商品添加到Redis中.
            Sku sku = skuFeign.findById(skuId).getData();
            System.out.println(sku.getSpuId());
            Spu spu = spuFeign.findSpuById(sku.getSpuId()).getData();

            //将sku信息转换成OrderItem
            orderItem = sku2OrderItem(sku, spu, num);
        }


        redisTemplate.boundHashOps(CART + username).put(skuId, orderItem);
    }

    /**
     * 购物车查询功能
     *
     * @param username
     * @return
     */
    @Override
    public Map list(String username) {
        Map map = new HashMap();
        //将订单项信息加入到Map集合中,而且需要将该订单项的数量和价格也放入到map中
        List<OrderItem> orderItemList = redisTemplate.boundHashOps(CART + username).values();
        map.put("orderItemList", orderItemList);

        Integer totalNum = 0;
        Integer totalPrice = 0;

        for (OrderItem orderItem : orderItemList) {
            totalNum += orderItem.getNum();
            totalPrice += orderItem.getMoney();
        }

        map.put("totalNum", totalNum);
        map.put("totalPrice", totalPrice);

        return map;
    }

    private OrderItem sku2OrderItem(Sku sku, Spu spu, Integer num) {
        OrderItem orderItem = new OrderItem();
        //分类Id设置
        orderItem.setCategoryId1(spu.getCategory1Id());
        orderItem.setCategoryId1(spu.getCategory2Id());
        orderItem.setCategoryId1(spu.getCategory3Id());

        orderItem.setSpuId(spu.getId());
        orderItem.setSkuId(sku.getId());
        orderItem.setName(sku.getName());
        orderItem.setPrice(sku.getPrice());
        orderItem.setNum(num);
        orderItem.setPayMoney(num * orderItem.getPrice());
        orderItem.setMoney(num * orderItem.getPrice());
        orderItem.setImage(sku.getImage());
        orderItem.setWeight(sku.getWeight());

        return orderItem;
    }
}
